﻿using System;
using System.Collections.Generic;
using System.Text;
using DataProvider;
using System.Data;
using System.Data.SqlClient;

namespace BusinessLayer
{
    public class CTPNhap : XL_Bang
    {
        #region thuoc tinh
        private string _soPN;

        public string SoPN
        {
            get { return _soPN; }
            set { _soPN = value; }
        }
        private string _maVTU;

        public string MaVTU
        {
            get { return _maVTU; }
            set { _maVTU = value; }
        }
        private string _soLuong;

        public string Soluong
        {
            get { return _soLuong; }
            set { _soLuong = value; }
        }
        private string _dgNhap;

        public string DGNhap
        {
            get { return _dgNhap; }
            set { _dgNhap = value; }
        }

        #endregion

        #region cac phuong thuc
        public DataTable GetData()
        {
            StrSQL = "select * from CTPNHAP";
            return GetDataTable();
        }
        public DataTable GetData(string soPN)
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "LOAD_CTPNHAP_BY_SOPN";
                command.Parameters.AddWithValue("@SOPN", soPN);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
                close();
                return table;
            }
            catch (Exception)
            {
                close();
                return null;
            }
        }
        public bool AddNew()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_CTPNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@SLNHAP", _soLuong);
                command.Parameters.AddWithValue("@DGNHAP", _dgNhap);
                command.Parameters.AddWithValue("@TYPE", 0);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                command.Dispose();
                close();
            }
        }
        public bool Edit()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_CTPNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@SLNHAP", _soLuong);
                command.Parameters.AddWithValue("@DGNHAP", _dgNhap);
                command.Parameters.AddWithValue("@TYPE",1);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        public bool Delete()
        {
            try
            {
                open();
                command = new System.Data.SqlClient.SqlCommand();
                command.CommandText = "INSERT_UPDATE_DELETE_CTPNHAP";
                command.Parameters.AddWithValue("@SOPN", _soPN);
                command.Parameters.AddWithValue("@MAVTU", _maVTU);
                command.Parameters.AddWithValue("@SLNHAP", string.Empty);
                command.Parameters.AddWithValue("@DGNHAP", string.Empty);
                command.Parameters.AddWithValue("@TYPE", 2);
                command.Connection = connect;
                command.CommandType = CommandType.StoredProcedure;
                command.ExecuteNonQuery();
                close();
                return true;
            }
            catch (Exception)
            {
                close();
                return false;
            }
        }
        #endregion

    }
}
